package com.spring.controller;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;

/**
 * 定义拦截器对象，基于此拦截器对指定请求进行拦截。
 */
public class TimeAccessInterceptor implements HandlerInterceptor {
    /**
     * PreHandle方法在@Controller方法执行之前执行
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("==preHandle==");
        LocalTime now = LocalTime.now();
        int hour = now.getHour();
        if(hour<=6||hour>=22){
            throw new RuntimeException("不在访问时间");
        }
        return true;//true表示放行，false或异常则表示请求到此结束
    }
}
